package cn.ipanel.bigdata

import cn.ipanel.bigdata.boot.date.{Date, Day}
import cn.ipanel.bigdata.boot.logger.Logger
import cn.ipanel.bigdata.boot.{Job, TIntent, TJoin}
import cn.ipanel.bigdata.dw.Const
import cn.ipanel.bigdata.utils.Ru
import cn.ipanel.bigdata.utils.Util.isEmpty
import org.apache.commons.lang3.exception.ExceptionUtils
import org.apache.spark.sql.DataFrame

/**
 * Author: lzz
 * Date: 2022/6/24 10:25
 */
package object job {

  abstract class AbsJob extends Job with Const with Ru with TIntent with TJoin {

//    final lazy val _YYYYMMDD: String = getDateFromParam
//    final lazy val _DAY: Day = if (isEmpty(_YYYYMMDD)) Date.asYesterday else Day(_YYYYMMDD)

    def optSourceRange: Int = 0
    lazy val _RANGE: Int = optSourceRange

    lazy val _INTENT: TIntent = {
      if (isEmpty(getTemplate.getIntent)) this
      else {
        try {
          clsInstance(getTemplate.getIntent).asInstanceOf[TIntent]
        } catch {
          case e: Exception =>
            Logger.E(s"""Create Intent Instance Failed.
                        | Because: ${ExceptionUtils.getStackTrace(e)}
                        |""".stripMargin)
            this
        }
      }
    }
    override def intent(data: DataFrame, day: Day): DataFrame = data

    lazy val _JOIN: TJoin = {
      if (isEmpty(getTemplate.getJoin)) this
      else {
        try {
          clsInstance(getTemplate.getJoin).asInstanceOf[TJoin]
        } catch {
          case e: Exception =>
            Logger.E(s"""Create Join Instance Failed.
                        | Because: ${ExceptionUtils.getStackTrace(e)}
                        |""".stripMargin)
            this
        }
      }
    }
    override def join(data: DataFrame, day: Day): DataFrame = data

    def source(): DataFrame
  }
}